<?php
//
// MessageModule.class.php
//

// Direct access protection
if( !defined( 'LIRIOPE' )) die( 'Direct access is not allowed.' );

class MessageModule Extends LiriopeModule {

  // show()
  // displays a list of the latest blog posts
  //
  public function show( $vars=NULL ) {
    global $module;
    $page = a::get($vars, 'page');

    // Hold the name of the custom view file if one was passed
    // look for a custom view
    if( a::get($vars, 'template')) {
      $template = a::get($vars, 'template');
      $seek = $this->_controller . '/_' . $this->_action . '-' . basename($template) . '.php';
      $customView = load::exists( $seek );
    }

    if( $customView ) {
        $module->useView($customView);
    }

    $message = new Messages( a::get($vars, 'yaml'));
    $message->setGATracking(" onClick=\"javascript:pageTracker._trackPageview('/internal-links/messages/%s');\"");

    $module->message = $message;
  }

  // currentSeries()
  //
  public function currentSeries( $vars=array() ) {
    global $module;

    $limit = a::get($vars, 'limit', 1);
    $recent = array();
    $count = 0;

    $blogs = new Blogs( c::get('root.content') . '/media/series/');
    $c = 0;
    do {
        if(++$c > 100) break;
        $current = $blogs->getNext();
    } while(strtotime($current->series['begins']) > time());

    $module->series_url = $current->url();
    $module->series = $current->series;
  }

  // recentSermons()
  //
  public function recentSermons( $vars=array() ) {
    global $module;

    $limit = a::get($vars, 'limit', 3);
    $recent = array();
    $count = 0;

    $blogs = new Blogs( c::get('root.content') . '/media/series/');

    while( $count < $limit ) {
      $last = $blogs->getNext();
      $series = new Messages( $last->series() );
      if(!$series->hasMessages()) continue;
      foreach( $series->getMessages() as $message) {
        if( $count >= $limit ) break;
        $recent[] = array( 'series'=>array('title'=>$series->getTitle(), 'url'=>$last->url(), 'chip'=>$series->getChipImage()),
          'message'=>$message );
        $count++;
      }
    }

    $module->sermons = $recent;
  }

}

?>
